<?php
namespace Home\Controller;
use Think\Controller;

//报告
class ReportController extends CommonController {	
	
	public function __construct(){
        parent::__construct();
        // 用户信息
        if($this->role != -1){// 游客
            $this->assign('user_email',session('user_email'));
            $this->user_token = session('user_token');
        }
	}

	//列表
	public function index(){
		$this->assign('head_title',"云途数据市场-数据报告");
		
		$this->display();
	}

	//详情
	public function detail(){
		$id = I('get.id',0,'intval');
		if(empty($id)) redirect("/report/index.html");

		$reportModel = D('Report');
		$uid = session('uid');
		$info = $reportModel->fullInfo($id,$uid);
		if(empty($info) || $info['status'] < 1){
			$this->error('该报告不存在','/report/index.html');
		}
		
		//相关数据
		$related_list = array();
		if(!empty($info['related_service_ids'])) {
			$related_id_array = explode(",",$info['related_service_ids']);
			$related_list = $this->getServiceWithRelated($related_id_array);
			if(!empty($related_list)) $related_list = array_slice($related_list,0,5);
		}

		// timp($related_list);die;
		$this->assign('related_list',$related_list);
		$this->assign('info',$info);
		$this->display();
	}

	//相关数据
	private function getServiceWithRelated($related_id_array = array()){
		if(empty($related_id_array)) return null;
        $ReportModel = D("Report");

        $service_list = $ReportModel->getRelatedServices($related_id_array);
        // timp($service_list);die;
        return $service_list;
	}

	//报告列表页-ajax列表数据
	public function ajaxList(){
		//报告列表
		$page = I('get.page',1,'intval');
		$ReportModel = D('Report');
		$list = $ReportModel->getList($page,$pagesize=10,$getcontent=false);

		$this->ajaxReturn( array('status'=>200,'msg'=>'OK','data'=>$list) );
	}

	// 立即下载，已购买，购买
	public function ajaxDownload(){
		$id = I('post.id',0,'intval');
		if(empty($id)) $this->ajaxReturn( array('status'=>404,'msg'=>'缺少必要参数','data'=>null) );
		
		$uid = session('uid');
		if(empty($uid)) $this->ajaxReturn( array('status'=>4040,'msg'=>'需要登录','data'=>null) );
		
		$reportModel = D('Report');
		$info = $reportModel->fullInfo($id,$uid);
		if(empty($info)) $this->ajaxReturn( array('status'=>404,'msg'=>'该报告不存在','data'=>null) );

		if($info['price'] >0 && $info['pay_status'] >0 ){
			//已购买
			$this->ajaxReturn( array('status'=>200,'data'=>200,'msg'=>'已购买') );
		}else if($info['price'] >0 ){
			//需要购买
			$this->ajaxReturn( array('status'=>200,'data'=>300,'msg'=>'购买') );
		}else{
			//免费下载
			$this->ajaxReturn( array('status'=>200,'data'=>100,'msg'=>'免费下载') );
		}
	}
}